﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAP.Middleware.Connector;
using WR.BL.BE;
using System.Configuration;

namespace WR.DL.DALC.SAP
{
    public sealed class DALC
    {
        //RGF 20111102
        private static string preFijo = getParamValue("ConnType").ToLower();
        private static string _uName = "";
        private static string _passwd = "";
        private static string _server = getParamValue(preFijo + "Ashost");
        private static string _instance = getParamValue(preFijo + "Instancenr");
        private static string _sysId = getParamValue(preFijo + "SysId");
        private static string _langu = getParamValue("Idioma");
        private static string _mandt = getParamValue(preFijo + "Client");
        private static string _sapRouter = getParamValue(preFijo + "Router");

        //Datos x default
        //private static string _uName = "ttapia";
        //private static string _passwd = "maestro1";
        //private static string _server = "172.16.40.14";
        //private static string _instance = "00";
        //private static string _sysId = "DEV";
        //private static string _langu = "ES";
        //private static string _mandt = getParamValue("MANDT");// "100";
        ////private static string _sapRouter = "/H/200.62.140.3/H/";

        public static string getParamValue(string key)
        {
            return ConfigurationManager.AppSettings[key.ToUpper()];
        }

        public static RfcConfigParameters getConfigParam(RfcConfigParametersBE objConn)
        {
            RfcConfigParameters foundDestination;
            foundDestination = new RfcConfigParameters();
            foundDestination.Add(RfcConfigParameters.Name, objConn.Id);
            foundDestination.Add(RfcConfigParameters.SAPRouter, (string.IsNullOrEmpty(objConn.SapRouter) ? _sapRouter : objConn.SapRouter));//RGF 20111102
            foundDestination.Add(RfcConfigParameters.AppServerHost, (string.IsNullOrEmpty(objConn.Server) ? _server : objConn.Server));
            foundDestination.Add(RfcConfigParameters.SystemNumber, (string.IsNullOrEmpty(objConn.Instance) ? _instance : objConn.Instance));
            foundDestination.Add(RfcConfigParameters.SystemID, (string.IsNullOrEmpty(objConn.SysId) ? _sysId : objConn.SysId));
            foundDestination.Add(RfcConfigParameters.User, (string.IsNullOrEmpty(objConn.Uname) ? _uName : objConn.Uname));
            foundDestination.Add(RfcConfigParameters.Password, (string.IsNullOrEmpty(objConn.Passwd) ? _passwd : objConn.Passwd));
            foundDestination.Add(RfcConfigParameters.Client, (string.IsNullOrEmpty(objConn.Mandt) ? _mandt : objConn.Mandt));
            foundDestination.Add(RfcConfigParameters.Language, (string.IsNullOrEmpty(objConn.Langu) ? _langu : objConn.Langu));
            foundDestination.Add(RfcConfigParameters.PoolSize, "1");
            foundDestination.Add(RfcConfigParameters.MaxPoolSize, "1");
            foundDestination.Add(RfcConfigParameters.IdleTimeout, "1");

            return foundDestination;
        }

        public static string getConnectionId(RfcConfigParametersBE pObjConn)
        {
            string retval = string.Empty;
            RfcDestination dest; RfcConfigParameters cp;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                cp = DALC.getConfigParam(pObjConn);
                try
                {
                    //RfcDestinationManager.UnregisterDestinationConfiguration(dc);
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                dc.AddOrEditDestination(cp);
                dest = RfcDestinationManager.GetDestination(pObjConn.Id);
                dest.Ping();
            }
            catch (Exception ex)
            {
                dc.RemoveDestination(pObjConn.Id);
                retval = ex.Message;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return retval;
        }

        public static string validaTienda(string pTienda, string pConnId, ref int vTienda)
        {
            IRfcFunction function = null;
            string nomTda = string.Empty;
            string valTda = string.Empty;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                RfcDestination dest = RfcDestinationManager.GetDestination(pConnId);
                function = dest.Repository.CreateFunction("ZWR_SD_013");
                function.SetValue("I_NOMTDA", pTienda);
                function.Invoke(dest);

                nomTda = function.GetValue("E_WERKS").ToString();
                vTienda = function.GetInt("E_RETURN");
            }
            catch (RfcCommunicationException e)
            {
                // network problem...
            }
            catch (RfcLogonException e)
            {
                // user could not logon...
            }
            catch (RfcAbapRuntimeException e)
            {
                // serious problem on ABAP system side...
            }
            catch (RfcAbapBaseException e)
            {
                // The function module returned an ABAP exception, an ABAP message
                // or an ABAP class-based exception...
            }
            catch (RfcBaseException e)
            {
                // Grabar Log de errores con e.message
                throw new Exception(e.Message);
                return null;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return nomTda;
        }

        public static string logOut(string pConnId)
        {
            string retval = string.Empty;
            InMemoryDestinationConfiguration dc = new InMemoryDestinationConfiguration();
            try
            {
                try
                {
                    RfcDestinationManager.RegisterDestinationConfiguration(dc);
                }
                catch { }
                try//fix para la ejecución en la pantalla inicio cuando aún no se han logeado.
                {
                    RfcDestination dest = RfcDestinationManager.GetDestination(pConnId);
                    dest.Repository.ClearAllMetadata();
                    dest.Parameters.Clear();
                }
                catch { }
                dc.RemoveDestination(pConnId);
            }
            catch (Exception ex)
            {
                retval = ex.Message;
            }
            finally
            {
                RfcDestinationManager.UnregisterDestinationConfiguration(dc);
            }
            return retval;
        }
    }
}
